package com.elmcode.server.controller.admin;

import com.elmcode.common.utils.PageUtils;
import com.elmcode.common.utils.R;
import com.elmcode.server.dto.FoodDTO;
import com.elmcode.server.entity.FoodEntity;
import com.elmcode.server.exception.ParamsException;
import com.elmcode.server.service.FoodService;
import com.elmcode.server.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 菜品
 *
 * @author laozj
 * @email laozj@elm.com
 * @date 2024-06-13 13:05:04
 */
@Api(tags = "食物接口")
@RestController
@RequestMapping("admin/food")
public class FoodController {

    @Autowired
    private FoodService foodService;

    /**
     * 列表
     */
    @GetMapping
    public Result list(@RequestParam Map<String, Object> params){
        PageUtils page = foodService.queryPage(params);

        return Result.ok("查询成功",page.getList(),page.getTotal());
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    public Result info(@PathVariable("id") Long id){
		FoodEntity food = foodService.getById(id);

        return Result.ok("获取成功",food);
    }

    /**
     * 保存
     */
    @PostMapping
    public R save(FoodDTO food){
		foodService.saveFood(food);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/{id}")
    public R update(@PathVariable("id")Long id,FoodDTO food){
		foodService.updateFoodInfo(id,food);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		foodService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 禁用/启用菜品接口
     */
    @ApiOperation("禁用/启用菜品接口")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable("id")Long id,@RequestParam String status) throws ParamsException {
        foodService.updateStatus(id,status);

        return Result.ok("更新状态成功");
    }

}
